const db = require('../db');
const pageSize = db.pageSize

function base(table) {
	const contruct = require(`./${table}`)
	const obj = contruct(db.sequelize, db.dataTypes)
	obj.sql = db.sequelize
	obj.SELECT = db.sequelize.QueryTypes.SELECT
	obj.getBy = (param) => {
		return obj.findOne({ where: param });
	}
	obj.findBy = (param) => {
		return obj.findAll({ where: param })
	}
	obj.findByOrder = (param, order) => {
		return obj.findAll({ where: param, order })
	}
	obj.pageByOrder = (param, order, pageNum) => {
		return obj.findAll({ where: param, order: order, offset: (pageNum - 1) * pageSize, limit: pageSize })
	}
	obj.findBySql = (sql, param) => {
		return obj.sql.query(sql, { replacements: param, type: obj.SELECT })
	}
	obj.updateBy = (model, param) => {
		return obj.update(model, { where: param })
	}
	obj.deleteBy = (param) => {
		return obj.destroy({ where: param })
	}
	obj.countBy = (param) => {
		return obj.count({ where: param })
	}
	obj.now = () => {
		let date = new Date();
		// date.setHours(date.getHours() + 8);
		return date
	}
	return obj

}
module.exports = base

